using MessagePack;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace David.CanBus
{
    using uint64_t = UInt64;
    using uint32_t = UInt32;
    using uint16_t = UInt16;
    using uint8_t = Byte;
    using int16_t = Int16;
    using int32_t = Int32;
    using CAN_BOOL = Byte;
    public enum RunMode_t : byte
    {
        Normal = 0,
        LoopBack = 1,
        Silent = 2,
        SilentLoopBack = 3,
    }
    public enum FilterMode_t : byte
    {
        IdMask,
        IdList,
    }
    public enum FilterScale_t : byte
    {
        Bit16,
        Bit32,
    }
    
    public enum FdMode_t : byte
    {
        ISO = 0,
        BOSCH = 1,
    }
    public enum FrameFormat_t : byte
    {
        Classic=0,
        FdNoBrs=1,
        FdBrs=2,
    }

    [Flags]
    public enum CANX_Packet_Flags : uint
    {
        Data = 1 << 0,
        Std = 1 << 1,
        Brs = 1 << 2,
        CanFd = 1 << 3,
        Esi = 1 << 4,
        Echo = 1 << 5,
    }

    [MessagePackObject]
    public partial class CANX_Packet_t
    {
        [Key(0)]
        public uint32_t Identifier;
        [Key(1)]
        public CANX_Packet_Flags Flags;
        [Key(2)]
        public uint8_t DataLen;
        [Key(3)]
        public uint8_t[] Data;
        [Key(4)]
        public uint8_t ChannelIndex;
        [Key(5)]
        public uint64_t TimeStamp;
        [Key(6)]
        public uint32_t Key;
        [Key(7)]
        public UInt64 UserData;

    }
   
    [MessagePackObject]
    public partial class BusRate_t
    {
        [Key(0)]
        public uint32_t Rate;
        [Key(1)]
        public uint32_t Prescaler;
        [Key(2)]
        public uint32_t Sjw;
        [Key(3)]
        public uint32_t Ts1;
        [Key(4)]
        public uint32_t Ts2;

        
    };

   
    [MessagePackObject]
    public partial class CanDeviceConfig_t
    {
        [Key(0)]
        public FrameFormat_t FrameFormat;
        [Key(1)]
        public bool IsEnableFilter;
        [Key(2)]
        public bool IsEnableChannel;
        [Key(3)]
        public RunMode_t RunMode;
        [Key(4)]
        public BusRate_t BaudRateConfig;
        [Key(5)]
        public uint32_t FrameId;
        [Key(6)]
        public uint32_t FrameIdMask;
        [Key(7)]
        public FdMode_t FdMode;
        [Key(8)]
        public BusRate_t FdDataRateConfig;
        
    }


    public struct DllInfo_t
    {
        public string DllVersion;
    }
    [MessagePackObject]
    public partial class DeviceInfo_t
    {
        [Key(0)]
        public uint8_t CanXChannelCount;
        [Key(1)]
        public string ProductModel;
        [Key(2)]
        public string HardwareVersion;
        [Key(3)]
        public string SoftwareVersion;
        [Key(4)]
        public string SerialNumber;


    }
    

    public enum DeviceEventTagType_t//占4字节
    {
        CanXRxOk,
        CanXTxReport,
        CanXBusState,
        CanXRxErrorEvent,
        EventQueueState,
    }

    [MessagePackObject]
    public partial class CANX_TxPacket_Report_t
    {
        [Key(0)]
        public CANX_Packet_t Packet;
        [Key(1)]
        public uint32_t ErrorCode;
        
    }


    public enum CANX_BusStateType_t
    {
        BUSOFF = 0x01,
        PASSIVE = 0x02,
        WARNING = 0x04,
        ACTIVE = 0x08,
    }
    
    [MessagePackObject]
    public partial class CanBusState_t
    {
        [Key(0)]
        public uint8_t ChannelIndex;
        [Key(1)]
        public CANX_BusStateType_t State;
        [Key(2)]
        public uint32_t TxErrorCounter;
        [Key(3)]
        public uint32_t RxErrorCounter;
        [Key(4)]
        public uint32_t SentCounter;
        [Key(5)]
        public uint32_t ReceivedCounter;
        [Key(6)]
        public uint64_t TxTimeNs;
        [Key(7)]
        public uint64_t RxTimeNs;
        [Key(8)]
        public uint32_t BusLoad;//0~10000
        [Key(9)]
        public uint64_t TimeStamp;
        
    }

    [MessagePackObject]
    public partial class CANX_RxErrorEvent_t
    {
        [Key(0)]
        public uint8_t ChannelIndex;
        [Key(1)]
        public CANX_Packet_Flags Flags;
        [Key(2)]
        public uint32_t ErrorCode;
        [Key(3)]
        public uint64_t TimeStamp;
        
    }


    [MessagePackObject]
    public partial class CANX_EventQueueState_t
    {
        [Key(0)]
        public uint32_t MaxItems;
        [Key(1)]
        public uint32_t CurrentItemCount;
       
    }


    [MessagePackObject]
    public partial class DeviceEvent_t
    {
        [Key(0)]
        public DeviceEventTagType_t EventTag;
        [Key(1)]
        public CANX_Packet_t CanXRx;
        [Key(2)]
        public CANX_TxPacket_Report_t CanXTxReport;
        [Key(3)]
        public CanBusState_t CanXBusState;
        [Key(4)]
        public CANX_RxErrorEvent_t CanXRxErrorEvent;
        [Key(5)]
        public CANX_EventQueueState_t EventQueueState;

        
    }
    
    public enum Can_ErrorMask_t : uint32_t
    {
        NONE = (0),             
        EWG = (1<<0),           
        EPV = (1<<1),           
        BOF = (1 << 2),         
        STF = (1 << 3),         
        FOR = (1 << 4),         
        ACK = (1 << 5),         
        BR = (1 << 6),          
        BD = (1 << 7),          
        CRC = (1 << 8),         
        RX_OV = (1 << 9),       
        TX_ALST = (1 << 10),    
        TX_TERR = (1 << 11),    
        NOT_READY = (1 << 12),  
        TX_BUSY = (1 << 13),
        DATA_PHASE = (1 << 14),
    }   

   
}
